#!/usr/bin/env python
#
#This module focuses on file i/o (note may be close related to data class)
#
import re
from data import *
class Settings():
    "This class loads and manages settings"
    d=dict()
    def __init__(self, Sfile='settings.cfg'):
            "set file name and load settings"
            self.FileName=Sfile
            self.readSettings(self.FileName)

    def readSettings(self, Sfile):
        "Read settings file into a dictionary"
        f = open(Sfile)
        s = f.read().splitlines()
        for r in s:
            if  not r.startswith('#'):
                l = re.split('=', r)
                if len(l)<2:
                    continue
                self.d[l[0]]=l[1]

    def get(self, par):
        "Returns value of the given param if it exists."
        return self.d[par]
    def set(self, par, val):
        "Assigns a new value to a param if exists"
        self.d[par]=val
    def save(self,  FileName='settings.cfg'):
        "Save current data to file"
        f = open(FileName,  'w')
        s=''
        for p, v in self.d.iteritems():
            s+= p  +  "="  +  v + '\n'
        f.write(s)
        f.close()
